package org.aalku.adbcopy;

import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class Launcher {
	public static void main(String[] args) throws IOException,
			InterruptedException {
		Engine engine = new Engine();
		List<String> devices = engine.getDevices();
		if (devices.isEmpty()) {
			throw new RuntimeException("Device not found");
		}
		if (devices.size() > 1) {
			throw new RuntimeException(
					"Several Android devices found, please leave only one connected");
		}

		String source = getSource(engine.getStorageDevices().toArray(new String[0]));
		String dest = getDest();
		String filePattern = ".*[.](jpg|jpeg|tiff|tif|tga|png|gif)$";

		engine.recursiveRetrive(source, dest, Pattern.compile(filePattern), true);
	}

	private static String getSource(String... options) {
         return (String) JOptionPane.showInputDialog(null, "Please select a storage device",
                 "Select source", JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
	}

	private static String getDest() {
		JFileChooser j = new JFileChooser();
		j.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int opt = j.showSaveDialog(null);
		if (opt != JFileChooser.APPROVE_OPTION) {
			System.err.println("Aborted by user");
			System.exit(1);
		}
		String dest = j.getSelectedFile().getPath();
		return dest;
	}

}
